import comm from './core';

function transformtv (data) {
  const {
    displayName,
    description,
    bgPic,
    lunboPic,
    smallPic,
    type,
    videoType,
    stat,
    totalEpisode,
    curEpisode
  } = data;

  const bigImage = bgPic || smallPic || lunboPic;
  const smallPics = smallPic || bgPic || lunboPic;
  return {
    title: displayName,
    intro: description,
    bigImage: comm.formatZoomPic(bigImage),
    smallPic: comm.formatZoomPic(smallPics),
    type,
    videoType,
    playCount: stat.playCount,
    curEpisode,
    totalEpisode
  };
}

export function get () {
  const params = comm.requestParams();
  const {
    origin,
    code,
    status
  } = params;

  if (!status || status !== 1) {
    return {};
  }
  const path = `program/findByCode`;
  const query = `code=${code}`;
  return comm.request({ origin, path, query }).then(({ data }) => {
    if (data) {
      return transformtv(data);
    }
  });
}

export default {
  get
};
